﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ControlUser_ControlNghiepVuDieuChinhTonKhoShowRoom : System.Web.UI.UserControl
{
    
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetLocation();
            ls_getAdjTrnNoList();
            if (Session["KhoHang"] != null)
            {
                Session["KhoHang"] = null;
            }
        }
    }
    void ls_getAdjTrnNoList()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getAdjTrnNoList", CommandType.StoredProcedure);
        DropSO.DataSource = db;
        DropSO.DataTextField = "Tex";
        DropSO.DataValueField = "Val";
        DropSO.DataBind();
    }
    void GetLocation()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getLocationList", CommandType.StoredProcedure);
        DropKho.DataSource = db;
        DropKho.DataTextField = "description";
        DropKho.DataValueField = "location";
        DropKho.DataBind();
    }
    protected void txtMaHang_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string str1 = txtMaHang.Text;
            int index = str1.IndexOf("|");
            txtMaHang.Text = str1.Substring(0, index - 1);
            txtTenHang.Text = str1.Substring(index + 2, str1.Length - index - 2);
            txtSL.Focus();
        }
        catch
        {
            return;
        }
    }
    protected void txtSL_TextChanged(object sender, EventArgs e)
    {
        if (txtSL.Text == "" || txtSL.Text == null)
        {

        }
        else
        {
            UpdateGrid();
            txtMaHang.Text = "";
            txtTenHang.Text = "";
            txtSL.Text = "";
            txtMaHang.Focus();
        }
    }
    void UpdateGrid()
    {
        DataTable table = new DataTable();
        if (Session["KhoHang"] != null)
        {
            table = (DataTable)Session["KhoHang"];
            foreach (DataRow dr in table.Rows)
            {
                if (dr[0].ToString() == txtMaHang.Text)
                {
                    dr[2] = Double.Parse(dr[2].ToString()) + Double.Parse(txtSL.Text);
                    Session["KhoHang"] = table;
                    GridView1.DataSource = Session["KhoHang"];
                    GridView1.DataBind();
                    return;
                }
            }

            DataRow row = table.NewRow();
            //Fill All Columns with Data
            row[0] = txtMaHang.Text;
            row[1] = txtTenHang.Text;
            row[2] = int.Parse(txtSL.Text);

            table.Rows.Add(row);
        }
        else
        {
           
            
            DataColumn col1 = new DataColumn("Mã hàng");
            DataColumn col2 = new DataColumn("Tên hàng");
            DataColumn col3 = new DataColumn("Số lượng");

            col1.DataType = System.Type.GetType("System.String");
            col2.DataType = System.Type.GetType("System.String");
            col3.DataType = System.Type.GetType("System.Double");
            
            table.Columns.Add(col1);
            table.Columns.Add(col2);
            table.Columns.Add(col3);

            //Create a Row in the DataTable table
            DataRow row = table.NewRow();
            //Fill All Columns with Data
            row[col1] = txtMaHang.Text;
            row[col2] = txtTenHang.Text;
            row[col3] = Double.Parse(txtSL.Text);

            table.Rows.Add(row);

            Session["KhoHang"] = table;
        }
        GridView1.DataSource = Session["KhoHang"];
        GridView1.DataBind();
    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            string DocDate = dt.GetDataDepend(str, "ls_getWorkingDay", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Location", DropKho.SelectedValue.ToString()));
            string DocNo = "";
            if (DropSO.SelectedValue.ToString() != "New")
            {
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_delAdjTrn", ref error,
                    new SqlParameter("@DocNo", DropSO.SelectedValue.ToString()));
                DocNo = DropSO.SelectedValue.ToString();
            }
            else
            {
                DocNo = dt.GetDataDepend(str, "ls_getNextAdjTrnNo", CommandType.StoredProcedure, ref error);
            }
            foreach (GridViewRow grv in GridView1.Rows)
            {
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_addNewAdjTrn", ref error,
                    new SqlParameter("@ItemKey", grv.Cells[0].Text),
                    new SqlParameter("@Location", DropKho.SelectedValue.ToString()),
                    new SqlParameter("@DocNo", DocNo),
                    new SqlParameter("@DocDate", DocDate),
                    new SqlParameter("@stockQty", int.Parse(grv.Cells[2].Text)));
            }
            uscMsgBox1.AddMessage("Lưu thông tin thành công", uscMsgBox.enmMessageType.Success);
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
        catch
        {
            uscMsgBox1.AddMessage("Lưu thông tin thất bại", uscMsgBox.enmMessageType.Success);
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
        Double soluong = Double.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
        DataTable table = new DataTable();
        table = (DataTable)Session["KhoHang"];

        foreach (DataRow dr in table.Rows)
        {
            if (dr[0].ToString() == keys)
            {
                dr[2] = soluong;
                table.AcceptChanges();
                Session["KhoHang"] = table;

                GridView1.EditIndex = -1;
                GridView1.DataSource = Session["KhoHang"];
                GridView1.DataBind();
            }
        }

        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
            string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();

            DataTable table = new DataTable();
            table = (DataTable)Session["KhoHang"];

            int i=0;
            foreach (DataRow dr in table.Rows)
            {
                if (dr[0].ToString() == keys)
                {
                    table.Rows[i].Delete();
                    table.AcceptChanges();
                    Session["KhoHang"] = table;
                    GridView1.EditIndex = -1;
                    GridView1.DataSource = Session["KhoHang"];
                    GridView1.DataBind();
                    return;
                }
                i++;
            }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = Session["KhoHang"];
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = Session["KhoHang"];
        GridView1.DataBind();
    }
    protected void DropSO_SelectedIndexChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db=dt.Get_Data_DataTable(str,ref error,"ls_getAdjTrnDetail",CommandType.StoredProcedure,
            new SqlParameter("@DocNo",DropSO.SelectedValue.ToString()));
        if (db.Rows.Count > 0)
        {
            Session["KhoHang"] = db;
            GridView1.DataSource = Session["KhoHang"];
            GridView1.DataBind();
        }
    }
}